package tcln.gxtx.client.utils.format;


/**
 * A newer version of {@link java.text.MessageFormat} which handles <code>null</code> parameters.
 * 
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public class MessageFormat extends java.text.MessageFormat {
    private static final long serialVersionUID = 1L;

    /**
     * @param pattern
     */
    public MessageFormat(String pattern) {
        super(pattern);
    }

    /**
     * Creates a {@link java.text.MessageFormat} instance with the given pattern and uses it to format the given
     * arguments.
     * <p>
     * <b>CAUTION</b>: We have to avoid passing the NULL arguments parameter to method
     * {@link java.text.MessageFormat#format(String, Object...)}, if not then raising the Javascript error (caused by
     * the GWTX compiler). Thus, fall <code>null</code> parameters will be replaced by empty strings.
     * 
     * @param pattern
     * @param parameters
     * @return
     */
    public static String format(String pattern, Object... parameters) {
        if (parameters == null) {
            return pattern;
        }

        for (int i = 0; i < parameters.length; i++) {
            if (parameters[i] == null) {
                parameters[i] = "";
            }
        }

        return java.text.MessageFormat.format(pattern, parameters);
    }
}
